package com.itheima.tlias.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.annotation.Log;
import com.itheima.tlias.dto.ReportDegreeCountDTO;
import com.itheima.tlias.dto.SelectAllStudentDTO;
import com.itheima.tlias.dto.StudentCountDTO;
import com.itheima.tlias.entity.Student;
import com.itheima.tlias.mapper.StudentMapper;
import com.itheima.tlias.po.InsertStudentParam;
import com.itheima.tlias.po.SelectStudentPageParam;
import com.itheima.tlias.service.StudentService;
import com.itheima.tlias.util.ResultUtil;
import com.itheima.tlias.vo.PageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentMapper studentMapper;

    @Resource
    StudentCountDTO dto;

    @Override
    public ResultUtil selectStudentByPage(SelectStudentPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectAllStudentDTO> dtos = studentMapper.selectStudentByPage(param);
        PageVO<SelectAllStudentDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return ResultUtil.success(pageVO);
    }

    @Log
    @Override
    public int insertStudent(InsertStudentParam param) {
        return studentMapper.insertStudent(param);
    }

    @Log
    @Override
    public int batchDeleteStudent(List<Integer> ids) {
        return studentMapper.batchDeleteStudent(ids);
    }

    @Override
    public Student selectStudentById(Integer id) {
        return studentMapper.selectStudentById(id);
    }

    @Log
    @Override
    public int updateStudent(Student param) {
        return studentMapper.updateStudent(param);
    }

    @Log
    @Override
    public int updateViolation(Integer id,Integer score) {
        Student student = studentMapper.selectStudentById(id);
        student.setUpdateTime(LocalDateTime.now());
        if(score >0) {
            student.setViolationCount(student.getViolationCount() + 1);
        }
        student.setViolationScore(student.getViolationScore()+score);
        return studentMapper.updateStudent(student);
    }

    @Override
    public List<ReportDegreeCountDTO> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }

    @Override
    public ResultUtil studentCountData() {
        List<StudentCountDTO> list = studentMapper.studentCountData();
        List<String> clazzlist = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        list.forEach(o -> {
            clazzlist.add(o.getClassName());
            dataList.add(o.getValue());
        });
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("clazzList",clazzlist);
        jsonObject.put("dataList",dataList);
        return ResultUtil.success(jsonObject);
    }
}
